﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace teacherplanner
{
    public partial class DefineTimetable : Form
    {
        OleDbConnection con;
        DataSet ds1;
        DataSet ds2;
        OleDbDataAdapter da1;
        OleDbDataAdapter da2;
        DataViewManager dsview1;

        public DefineTimetable()
        {
            InitializeComponent();
        }

        private void DefineTimetable_Load(object sender, EventArgs e)
        {
            con = new System.Data.OleDb.OleDbConnection();
            ds1 = new DataSet();
            con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= " + teacherplanner.Properties.Resources.CurrentPlanner;
            string sql1 = "SELECT * FROM Classes";
            da1 = new OleDbDataAdapter(sql1, con);

            con.Open();

            da1.Fill(ds1, "Classes");

            dsview1 = ds1.DefaultViewManager;
            className.DataSource = dsview1;
            className.DisplayMember = "Classes.ClassName";
            className.ValueMember = "Classes.ClassID";

            className.SelectAll();

            con.Close();
            con.Dispose();

        }

        private void Box_Click(object sender, EventArgs e)
        {
            Label myTb = (Label)sender;
            if (myTb.Text != "")
            {
                myTb.Text = "";
                myTb.Tag = "";
            }
            else
            {
                myTb.Text = className.SelectedText;
                myTb.Tag = className.SelectedValue;
            }
        }

        private void save_btn_Click(object sender, EventArgs e)
        {
            String[] InsertArray = new String[45];
            int a = 1;
            DataBase.Edit.CreateTable("CREATE TABLE Timetable (TimetableID INT IDENTITY(1,1) NOT NULL, DayoftheWeek INT, DayLessonNum INT, ClassID INT)");
            for (int i = 1; i < 7; i++)
            {
                int jmax;
                if (i == 6)
                { jmax = 5; }
                else
                { jmax = 8; }
                for (int j = 1; j <= jmax; j++)
                {
                    String CurLabelString = "day" + i + "les" + j;
                    Control[] CurLabelArr = this.Controls.Find(CurLabelString, true);
                    Label CurLabel = (Label)CurLabelArr[0];
                    if (CurLabel.Tag != null)
                    {
                        InsertArray[a - 1] = @"INSERT INTO Timetable VALUES (" + a + "," + i + "," + j + "," + CurLabel.Tag + ")";
                    }
                    else
                    {
                        InsertArray[a - 1] = @"INSERT INTO Timetable (TimetableID, DayoftheWeek, DayLessonNum) VALUES (" + a + "," + i + "," + j + ")";
                    }
                    a++;
                }

            }
            DataBase.Edit.ApplySQLArray(InsertArray);
            con = new System.Data.OleDb.OleDbConnection();
            ds2 = new DataSet();
            string sql2 = "SELECT * FROM TeachingDays";
            da2 = new OleDbDataAdapter(sql2, con);
            con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= " + teacherplanner.Properties.Settings.Default.CurrentPlanner;
            con.Open();

            da2.Fill(ds2, "TeachingDays");

            con.Close();
            con.Dispose();
            
            a = 1;
            DataBase.Edit.CreateTable("CREATE TABLE LessonDetails (LessonID INT IDENTITY(1,1) NOT NULL, DayID INT, DayLessonNum INT, ClassText VarChar(25))"); 
            
            int TDRowsMax = ds2.Tables["TeachingDays"].Rows.Count;
            int DayoftheWeek;
            int numSat=0;
            int TeachingDay;
            int lesMax;


            for (int TDRow = 0; TDRow < TDRowsMax; TDRow++)
            {
                DataRow dRow = ds2.Tables["TeachingDays"].Rows[TDRow];
                DayoftheWeek = (int)dRow.ItemArray.GetValue(2);
                if (DayoftheWeek == 6)
                {
                    numSat++;
                }
            }

            int strArrLen = (TDRowsMax - numSat) * 8 + (numSat) * 5;

            String[] InsertArray2 = new String[strArrLen];

            for (int TDRow = 0; TDRow < TDRowsMax; TDRow++)
            {
                DataRow dRow = ds2.Tables["TeachingDays"].Rows[TDRow];
                TeachingDay = (int)dRow.ItemArray.GetValue(0);
                DayoftheWeek = (int)dRow.ItemArray.GetValue(2);
  
                if (DayoftheWeek == 6)
                {
                    lesMax = 5;
                }
                else
                {
                    lesMax = 8;
                }

                for (int i = 1; i <= lesMax; i++)
                {
                    String CurLabelString = "day" + DayoftheWeek + "les" + i;
                    Control[] CurLabelArr = this.Controls.Find(CurLabelString, true);
                    Label CurLabel = (Label)CurLabelArr[0];
                    if (CurLabel.Tag != null)
                    {
                        InsertArray2[a - 1] = @"INSERT INTO LessonDetails VALUES (" + a + "," + TeachingDay + "," + i + ",'" + CurLabel.Text + "')";
                    }
                    else
                    {
                        InsertArray2[a - 1] = @"INSERT INTO LessonDetails (LessonID, DayID, DayLessonNum) VALUES (" + a + "," + TeachingDay + "," + i + ")";
                    }
                    a++;
                }
            }
            DataBase.Edit.ApplySQLArray(InsertArray2);
            DefineTimetable.ActiveForm.Hide();
            teacherplanner.PlannerForm myForm = new teacherplanner.PlannerForm();
            myForm.Show();
        }
    }
}
